﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;
using FinanSoft.Utilities;

namespace FinanSoft
{
    public partial class frmCrearMoneda : Form
    {

        private int _editedCurrencyId;

        public frmCrearMoneda(int editedCurrencyId)
        {
            InitializeComponent();
            finalDatePicker.Value = finalDatePicker.Value.AddDays(1);
            _editedCurrencyId = editedCurrencyId;
            fillFields();
        }

        private void fillFields()
        {
            if (_editedCurrencyId != Constants.INVALID_ID)
            {
                Currency editedCurrency = MoneyManager.GetInstance().GetCurrency(_editedCurrencyId);
                nameTextBox.Text = editedCurrency.Name;
                abrTextBox.Text = editedCurrency.Abbreviation;
                symbolTextBox.Text = editedCurrency.Symbol;
            }
            if (_editedCurrencyId == MoneyManager.GetInstance().SystemCurrency.Id)
            {
                btnActInfo.Enabled = false;
                nameTextBox.Enabled = false;
                abrTextBox.Enabled = false;
                symbolTextBox.Enabled = false;
            }
        }

        private void createCurrency()
        {
            _editedCurrencyId = MoneyManager.GetInstance().AddCurrency(
                symbolTextBox.Text, nameTextBox.Text, abrTextBox.Text);
            if(MoneyManager.GetInstance().IsError)
                MessageBox.Show(MoneyManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                MessageBox.Show(MessageConstants.CURRENCY_INSERTED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void editCurrency()
        {
            MoneyManager.GetInstance().EditCurrency(
                symbolTextBox.Text, nameTextBox.Text, abrTextBox.Text, _editedCurrencyId);
            if (MoneyManager.GetInstance().IsError)
                MessageBox.Show(MoneyManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                MessageBox.Show(MessageConstants.CURRENCY_UPDATED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private bool validateCurrencyFields()
        {
            if (symbolTextBox.Text.Trim().Length == 0 || abrTextBox.Text.Trim().Length == 0 || nameTextBox.Text.Trim().Length == 0)
            {
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (abrTextBox.Text.Trim().Length != Constants.CURRENCY_ABBREVIATION_LENGTH)
            {
                MessageBox.Show(MessageConstants.ABR_MUST_BE_3, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
                return true;
        }

        private bool validateExchangeRateFields()
        {
            if (exchangeRateTextBox.Text.Trim().Length == 0)
            {
                MessageBox.Show(MessageConstants.MISSING_FIELDS, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (initialDatePicker.Value.AddSeconds(1) > finalDatePicker.Value)
            {
                MessageBox.Show(MessageConstants.GREATER_INITIAL_DATE, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
            {
                try
                {
                    Convert.ToDouble(exchangeRateTextBox.Text);
                    return true;
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(MessageConstants.NEEDED_DECIMAL_NUMBER, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnActInfo_Click(object sender, EventArgs e)
        {
            if(validateCurrencyFields())
                if (_editedCurrencyId == Constants.INVALID_ID)
                    createCurrency();
                else
                    editCurrency();
        }

        private void btnActCambio_Click(object sender, EventArgs e)
        {
            if (validateExchangeRateFields())
            {
                MoneyManager.GetInstance().EstablishNewExchangeRate
                    (_editedCurrencyId, initialDatePicker.Value, finalDatePicker.Value, Convert.ToDouble(exchangeRateTextBox.Text));
                if (MoneyManager.GetInstance().IsError)
                    MessageBox.Show(MoneyManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(MessageConstants.EXCHANGE_RATE_ESTABLISHED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }
}
